﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using EHTWebsiteProject.Code.BLL;
using System.Net;
using EHTWebsite.Code;

namespace EHTWebsiteProject
{
    public partial class CustomerDownloadDocument : System.Web.UI.Page
    {
        private Int32 pageIndex = 1;
        private Int32 pageSize = 5;
        private Int32 totalRecord;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                pageIndex = Int32.Parse(Request.QueryString["PageIndex"].ToString());
            }
            catch (Exception)
            {
                pageIndex = 1;
            }

            pageSize = this.PaggingControl1.PageSize;

            if (!Page.IsPostBack)
            {
                GetAllDocuments();
            }
        }

        private void GetAllDocuments()
        {
            DataSet dsDocument = FileProvider.GetFiles(pageIndex, pageSize, out totalRecord);
            this.rpDocuments.DataSource = dsDocument;
            this.rpDocuments.DataBind();

            this.PaggingControl1.PageURL = "KundeLastNedDokument.html";
            this.PaggingControl1.TotalRecord = totalRecord;
            this.PaggingControl1.PagingSize = pageSize;
        }

        protected void rpDocuments_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            String cmdArgument = e.CommandArgument.ToString();
            String CmdName = cmdArgument.ToString().Split('.')[1];

            if (CmdName == "doc" || CmdName == "DOC")
            {
                DownloadFile(cmdArgument, true, ".doc");
            }
            else
            {
                if (CmdName == "xls" || CmdName == "XLS")
                {
                    DownloadFile(cmdArgument, true, ".xls");
                }
                else
                {
                    if (CmdName == "ppt" || CmdName == "PPT")
                    {
                        DownloadFile(cmdArgument, true, ".ppt");
                    }
                    else
                    {
                        if (CmdName == "docx" || CmdName == "DOCX")
                        {
                            DownloadFile(cmdArgument, true, ".docx");
                        }
                        else
                        {
                            if (CmdName == "xlsx" || CmdName == "XLSX")
                            {
                                DownloadFile(cmdArgument, true, ".xlsx");
                            }
                            else
                            {
                                if (CmdName == "pptx" || CmdName == "PPTX")
                                {
                                    DownloadFile(cmdArgument, true, ".pptx");
                                }
                                else
                                {
                                    if (CmdName == "pdf" || CmdName == "PDF")
                                    {
                                        DownloadFile(cmdArgument, true, ".pdf");
                                    }
                                    else
                                    {
                                        if (CmdName == "txt" || CmdName == "TXT")
                                        {
                                            DownloadFile(cmdArgument, true, ".txt");
                                        }
                                        else
                                        {
                                            if (CmdName == "rar" || CmdName == "RAR")
                                            {
                                                DownloadFile(cmdArgument, true, ".rar");
                                            }
                                            else
                                            {
                                                if (CmdName == "zip" || CmdName == "ZIP")
                                                {
                                                    DownloadFile(cmdArgument, true, ".zip");
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        private void DownloadFile(String fname, bool forceDownload, String sExt)
        {
            string sPath = Server.MapPath("~/ImageDatas/Files/");
            String sName = sPath + fname;
            string type = "";
            // set known types based on file extension  
            if (sExt != null)
            {
                switch (sExt.ToLower())
                {
                    case ".htm":
                    case ".html":
                        type = "text/HTML";
                        break;

                    case ".txt":
                        type = "text/plain";
                        break;

                    case ".doc":
                    case ".rtf":
                        type = "Application/msword";
                        break;
                    case ".docx":
                        type = "Application/vnd.ms-word.document.12";
                        break;
                    case ".xls":
                        type = "Application/ms-exel";
                        break;
                    case ".xlsx":
                        type = "Application/vnd.ms-excel.12";
                        break;
                    case ".ppt":
                        type = "Application/vnd.ms-powerpoint";
                        break;
                    case ".pdf":
                        type = "Application/pdf";
                        break;
                    case ".zip":
                        type = "Application/zip";
                        break;
                    case ".rar":
                        type = "application/x-rar-compressed";
                        break;
                    case ".pptx":
                        type = "Application/vnd.openxmlformats-officedocument.presentationml.presentation";
                        break;
                }
            }
            if (forceDownload)
            {
                Response.AppendHeader("content-disposition",
                    "attachment; filename=" + fname);
            }
            if (type != "")
                Response.ContentType = type;
            Response.WriteFile(sPath + fname);
            Response.End();
        }

        protected void rpDocuments_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            for (int i = 0; i < rpDocuments.Items.Count; i++)
            {
                ImageButton ibDownload = (ImageButton)rpDocuments.Items[i].FindControl("ibDownload");
                Label lbUrl = (Label)rpDocuments.Items[i].FindControl("lbDocumentUrl");

                String sType = Path.GetExtension(lbUrl.Text);
                if (sType.ToLower() == ".txt")
                {
                    ibDownload.ImageUrl = "~/ImageDatas/DownloadedIcons/txt-icon.png";
                }
                else
                {
                    if (sType.ToLower() == ".doc" || sType.ToLower() == ".docx")
                    {
                        ibDownload.ImageUrl = "~/ImageDatas/DownloadedIcons/ms_icon_small.jpg";
                    }
                    else
                    {
                        if (sType.ToLower() == ".xls" || sType.ToLower() == ".xlsx")
                        {
                            ibDownload.ImageUrl = "~/ImageDatas/DownloadedIcons/excel_icon.png";
                        }
                        else
                        {
                            if (sType.ToLower() == ".pdf")
                            {
                                ibDownload.ImageUrl = "~/ImageDatas/DownloadedIcons/pdf_icon_download.gif";
                            }
                            else
                            {
                                if (sType.ToLower() == ".zip")
                                {
                                    ibDownload.ImageUrl = "~/ImageDatas/DownloadedIcons/zip.png";
                                }
                                else
                                {
                                    if (sType.ToLower() == ".rar")
                                    {
                                        ibDownload.ImageUrl = "~/ImageDatas/DownloadedIcons/rar.jpeg";
                                    }
                                    else
                                    {
                                        if (sType.ToLower() == ".ppt" || sType.ToLower() == ".pptx")
                                        {
                                            ibDownload.ImageUrl = "~/ImageDatas/DownloadedIcons/pptx.jpeg";
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}